
const db = wx.cloud.database();
const shopcarCollection = db.collection('shopcar');
const productsCollection = db.collection('products');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    shop_num: 0,
    checked: false,
    checked_sub: false,
    result: [],
    img_url: 'https://oss.suning.com/vss/activity/wximg/cart/cshop.png',
    num: [],
    products: [],
    product: [],
    total_price: 0*100,
    falg: false,
  },
  onChangeOfHD(event) {
    let index = 0;
    let total =0;
    this.setData({
      checked: event.detail,
      flag: !this.data.flag
    });
    this.data.products.forEach(element => {
      const change = "products[" + index + "].checked_son";
      this.setData({
        [change]: !this.data.flag
      })
      if(this.data.products[index].checked_son){
        total = total + this.data.products[index].price*this.data.products[index].number;
      }
      index = index+1
    });
    if(this.data.products[0].checked_son){
      this.setData({
        total_price:total*100
      })
    }else{
      this.setData({
        total_price:0*100
      })
    }
    if (this.data.checked) {
      this.setButton()
    } else {
      this.setData({
        result: []
      })
    } 

  },
  onChangeOfSub(event) {
    this.setData({
      checked_sub: event.detail
    })
    if (this.data.checked_sub) {
      this.setButton()
      this.setData({
        checked: true,
      })
    } else {
      this.setData({
        checked: false,
        result: []
      })
    }
  },
  onChange(event) {
    const priceall = this.data.total_price;
    let {index} = event.currentTarget.dataset;
    const change = "products[" + index + "].checked_son";
    this.setData({
      result: event.detail,
      [change]: !this.data.products[index].checked_son
    });
   
  
    if(this.data.products[index].checked_son){
      this.setData({
        total_price:priceall+ this.data.products[index].price*this.data.products[index].number*100
      })
    }else{
      this.setData({
        total_price:priceall- this.data.products[index].price*this.data.products[index].number*100
      })
    }
   
  },
  count_totalprice(){
    let productinfo = this.data.products;
    console.log(productinfo);
    let totalprice = 0
    productinfo.forEach(element => {
      if(element.checked_son){
        totalprice = totalprice+ element.price*element.number*100
      }
    });
    this.setData({
      total_price:totalprice
    })
  },
  async reduce(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      num: e.currentTarget.dataset.num
    })
    let id = e.currentTarget.dataset.id;
    let { data } = await shopcarCollection
      .where({
        id: id
      })
      .get()
    let _id = data[0]._id;
    if (this.data.num < 1) {
      wx.showToast({
        title: '不能在减少了'
      });
      this.data.num = 1
    }
    shopcarCollection.doc(_id).update({
      data: {
        number: --this.data.num,
      },
    })
    const change = "products[" + index + "].number";
    this.setData({
      [change]: this.data.num
    })
    this.count_totalprice();
   
  },
  async add(e) {
    let index = e.currentTarget.dataset.index;
    
   
    this.setData({
      num: e.currentTarget.dataset.num
    })
    let id = e.currentTarget.dataset.id;
    let { data } = await shopcarCollection
      .where({
        id: id
      })
      .get()
    let _id = data[0]._id;
    if (this.data.num > 5) {
      wx.showToast({
        title: '亲~不野性消费',
      })
    }
    shopcarCollection.doc(_id).update({
      data: {
        number: ++this.data.num,
      },
    })
    const change = "products[" + index + "].number";
    this.setData({
      [change]: this.data.num
    })
    this.count_totalprice();
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  async resetdate() {
    let { data } = await shopcarCollection
      .orderBy('datatime', 'desc')
      .get()
  
    let thingCarts = [];
    data.forEach(item => {
      let message = {
        checked_son: false,
        ...item
      }
      thingCarts.push(message);
    })
    this.setData({
      products: thingCarts,
    })
  },
  setButton() {
    let result = [];
    for (let index in this.data.products) {
      result.push(index);
    }
    this.setData({
      result: result
    })
  },
  async onLoad(options) {
    this.resetdate()
    let { data: product } = await productsCollection
      .limit(10)
      .get();
    this.setData({
      product,
    })
    this.setButton()
    this.setData({
      shop_num: this.data.result.length,
      checked: true,
      checked_sub: true,
      total_price:0*100,
      flag: false
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})


